﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace EncodingStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            打印所有编码规则总数();

            打印默认编码规则();

            将string转换为byte数组和将byte数组转换为string采用不同的编码规则();

            将string转换为byte数组使用了错误了编码规则();

            读取包含了ANSI字符的ANSI编码文件();

            语言的字符串采用的编码();
        }

        public static void 打印所有编码规则总数()
        {
            Console.WriteLine(string.Format("系统支持【{0}】条编码规则。", Encoding.GetEncodings().Count()));
        }

        public static void 打印默认编码规则()
        {
            Console.WriteLine(string.Format("系统默认编码规则：【{0}】。", Encoding.Default.EncodingName));
        }

        public static void 将string转换为byte数组和将byte数组转换为string采用不同的编码规则()
        {
            Console.WriteLine("\n***" + System.Reflection.MethodInfo.GetCurrentMethod().Name + "***");

            string originalString = "Hello Test, 测试!";

            byte[] utf8Bytes = Encoding.UTF8.GetBytes(originalString);
            string errorString = Encoding.ASCII.GetString(utf8Bytes);

            Console.WriteLine("原始字符串：【{0}】。", originalString);
            Console.WriteLine("用UTF8编码，用ASCII解码后的错误字符串：【{0}】。", errorString);
        }

        public static void 将string转换为byte数组使用了错误了编码规则()
        {
            Console.WriteLine("\n***" + System.Reflection.MethodInfo.GetCurrentMethod().Name + "***");

            string originalString = "Hello Test, 测试!";

            byte[] asciiBytes = Encoding.ASCII.GetBytes(originalString);
            string errorString = Encoding.ASCII.GetString(asciiBytes);

            Console.WriteLine("原始字符串：【{0}】。", originalString);
            Console.WriteLine("用ASCII编码，用ASCII解码后的错误字符串：【{0}】，因为字符串中包含非ASCII字符。", errorString);
        }

        public static void 读取包含了ANSI字符的ANSI编码文件()
        {
            Console.WriteLine("\n***" + System.Reflection.MethodInfo.GetCurrentMethod().Name + "***");

            var file = @"E:\Coding\HappyStudy\EncodingStudy\EncodingStudy\ANSI.txt";
            Console.WriteLine("使用GB2312编码读取的内容：" + File.ReadAllText(file, Encoding.GetEncoding("GB2312")));
        }

        public static void 语言的字符串采用的编码()
        {
            Console.WriteLine("\n***" + System.Reflection.MethodInfo.GetCurrentMethod().Name + "***");

            Console.WriteLine("\u6BB5");
            Console.WriteLine("\x6BB5");

            string originalString = "hi 段";

            Console.WriteLine(
                String.Join(
                    ",",
                    originalString
                    .SelectMany(x => new byte[] { (byte)(x), (byte)(x >> 8) })
                    .Select(x => Convert.ToString(x, 16))
                )
            );

            Console.WriteLine(
                String.Join(
                    ",",
                    Encoding.Unicode
                    .GetBytes(originalString)
                    .Select(x => Convert.ToString(x, 16))
                )
            );

            Console.WriteLine(
                String.Join(
                    ",",
                    Encoding.UTF8
                    .GetBytes(originalString)
                    .Select(x => Convert.ToString(x, 16))
                )
            );

            Console.WriteLine(
                String.Join(
                    ",",
                    Encoding.UTF32
                    .GetBytes(originalString)
                    .Select(x => Convert.ToString(x, 16))
                )
            );
        }
    }
}
